﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Upeu.Matricula.Data.Entities;
//Alvaro Huanca Perez
namespace Upeu.Matricula.Data.Business
{
    public class TutorBusiness
    {
        //CRUD + Logic business or constrains
        public static IList<Tutor> GetListByFilter(string filter)
        {
            //throw new NotImplementedException();
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext()) // conexion a la db
            {
                return ctx.Tutor.Where(t => t.Nombre.Contains(filter)).ToList();
            }
        }
        public static Tutor Create(Tutor tutor)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Tutor tutorDB = ctx.Tutor.Add(tutor);
                ctx.SaveChanges();
                return tutorDB;
            }
        }
        public static Tutor Update(Tutor tutor)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                ctx.Entry(tutor).State = EntityState.Modified;
                ctx.SaveChanges();
                return tutor;
            }
        }
        public static Tutor GetById(Guid Tutor_Id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Tutor c = ctx.Tutor.Find(Tutor_Id);
                if (c == null)
                {
                    throw new Exception("Tutor no found in data base.");
                }
                return c;
            }
        }
        public static void Delete(Guid Tutor_Id)
        {
            using (UpeuMatriculaDbContext ctx = new UpeuMatriculaDbContext())
            {
                Tutor c = ctx.Tutor.Find(Tutor_Id);
                if (c == null)
                {
                    throw new Exception("Tutor no found in data base.");
                }
                ctx.Tutor.Remove(c);
                ctx.SaveChanges();
            }
        }
    }
}